package com.requiem.hist_group_manage_springboot.common;

import cn.hutool.core.util.StrUtil;
import com.requiem.hist_group_manage_springboot.constants.SystemConstants;
import com.requiem.hist_group_manage_springboot.exception.R;
import com.requiem.hist_group_manage_springboot.mapper.GroupLogMapper;
import com.requiem.hist_group_manage_springboot.mapper.GroupMapper;
import com.requiem.hist_group_manage_springboot.mapper.StudentMapper;
import com.requiem.hist_group_manage_springboot.mapper.TeacherMapper;
import com.requiem.hist_group_manage_springboot.model.GroupLogModel;
import com.requiem.hist_group_manage_springboot.model.GroupModel;
import com.requiem.hist_group_manage_springboot.model.StudentModel;
import com.requiem.hist_group_manage_springboot.model.TeacherModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class LogUtil {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    GroupMapper groupMapper;
    @Autowired
    GroupLogMapper groupLogMapper;

    public R recordStudent(int studentId, int groupId, int type) {
        StudentModel student = studentMapper.getStudentByID(studentId); // 获取学生信息（含姓名）
        GroupModel group = groupMapper.getGroupByID(groupId); // 获取小组信息（含名称）
        if (student == null) {
            return R.error(SystemConstants.DATA_DUPLICATE, SystemConstants.DATA_DUPLICATE_MSG);
        }
        if (group == null) {
            return R.error(SystemConstants.DATA_DUPLICATE, SystemConstants.DATA_DUPLICATE_MSG);
        }
        String fill = type == 1 ? "进入" : "退出";
        Integer id = type == 1 ? groupId : null;
        student.setGroupId(id);
        studentMapper.editStudent(student);
        LocalDateTime operateTime = LocalDateTime.now(); // 操作时间
        LocalDateTime formattedTime = operateTime.truncatedTo(ChronoUnit.SECONDS);

        // 2. 根据日志类型拼接日志内容
        String log = StrUtil.format("{}{}的{}学生{}在{}时{}{}", student.getDepartment(), student.getMajor(), student.getGrade(), student.getName(), formattedTime, fill, group.getName());
        // 3. 构造日志对象
        GroupLogModel logModel = new GroupLogModel();
        logModel.setGroupId(groupId);
        logModel.setStudentId(studentId);
        logModel.setLog(log); // 拼接好的日志内容

        logModel.setLogType(type);
        logModel.setCreateTime(formattedTime);
        int i = groupLogMapper.addGroupLog(logModel);
        if (i < 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        return R.success();
    }

    public void recordTeacher(int teacherId, int groupId, int type) {
        TeacherModel teacher = teacherMapper.getTeacherByID(teacherId); // 获取学生信息（含姓名）
        GroupModel group = groupMapper.getGroupByID(groupId); // 获取小组信息（含名称）
        String fill = type == 1 ? "进入" : "退出";
        String originalGroupId = teacher.getGroupId();
        List<Integer> idList = new ArrayList<>();

        if (originalGroupId != null && !originalGroupId.isEmpty()) {
            String[] ids = originalGroupId.split(",");
            for (String s : ids) {
                idList.add(Integer.parseInt(s));
            }
        }
        if (type == 1) {
            if (!idList.contains(groupId)) {
                idList.add(groupId);
            }
        } else if (type == 2) {
            Integer target = groupId;
            idList.remove(target);
        }

        String newGroupId = idList.stream().map(String::valueOf).collect(Collectors.joining(","));

        teacher.setGroupId(newGroupId);
        teacherMapper.editTeacher(teacher);
        LocalDateTime operateTime = LocalDateTime.now(); // 操作时间
        LocalDateTime formattedTime = operateTime.truncatedTo(ChronoUnit.SECONDS);

        // 2. 根据日志类型拼接日志内容
        String log = StrUtil.format("老师{}在{}时{}{}", teacher.getName(), formattedTime, fill, group.getName());
        // 3. 构造日志对象
        GroupLogModel logModel = new GroupLogModel();
        logModel.setGroupId(groupId);
        logModel.setTeacherId(teacherId);
        logModel.setLog(log); // 拼接好的日志内容
        logModel.setLogType(type);
        logModel.setCreateTime(formattedTime);
        groupLogMapper.addGroupLog(logModel);
    }
}
